//
//  RMBrowseShortNoteViewController.m
//  RMNote
//
//  Created by 晓风残月 on 16/4/19.
//  Copyright © 2016年 lxf. All rights reserved.
//

#import "RMBrowseShortNoteViewController.h"
#import "RMShortNoteCollectionViewCell.h"
#import "RMDateBaseManager.h"
#import "RMBaseModel.h"
#import "RMWriteShortNoteViewController.h"

#import <SDWebImage/UIImageView+WebCache.h>
#import <CHTCollectionViewWaterfallLayout.h>
@interface RMBrowseShortNoteViewController ()<UICollectionViewDataSource,CHTCollectionViewDelegateWaterfallLayout>
@property (weak, nonatomic) IBOutlet UICollectionView *ShortNoteCollectionView;
@property (nonatomic , strong) RMShortNoteCollectionViewCell *cell;
@property (nonatomic , strong) NSMutableArray *dataArr;
@end


@implementation RMBrowseShortNoteViewController


static NSString * const reuseIdentifier = @"Cell";

- (void)viewDidLoad {
    [super viewDidLoad];
    self.title = @"便签";
    CHTCollectionViewWaterfallLayout *layout = [[CHTCollectionViewWaterfallLayout alloc]init];
    layout.sectionInset = UIEdgeInsetsMake(10, 10, 10,10);
    
    self.ShortNoteCollectionView.collectionViewLayout = layout;
    
    self.ShortNoteCollectionView.delegate = self;
    self.ShortNoteCollectionView.dataSource = self;
    self.ShortNoteCollectionView.backgroundColor = [UIColor whiteColor];
    
    [self.ShortNoteCollectionView  registerNib:[UINib nibWithNibName:@"RMShortNoteCollectionViewCell" bundle:nil] forCellWithReuseIdentifier:reuseIdentifier];
    
    UILongPressGestureRecognizer *press = [[UILongPressGestureRecognizer alloc]initWithTarget:self action:@selector(LongPressAction:)];
    [self.ShortNoteCollectionView addGestureRecognizer:press];
 
}

- (void)viewWillAppear:(BOOL)animated{
    self.dataArr = [NSMutableArray array];
    self.dataArr = [[RMDateBaseManager defaultManager]selectNoteFromTable:kRMShortNoteTable].mutableCopy;    [self.ShortNoteCollectionView reloadData];
}
- (void)viewDidAppear:(BOOL)animated{
    [super viewDidAppear:animated];
    [self updateLayoutForOrientation:[UIApplication sharedApplication].statusBarOrientation];
}
//横屏
- (void)willAnimateRotationToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation duration:(NSTimeInterval)duration{
    [super willAnimateRotationToInterfaceOrientation:toInterfaceOrientation duration:duration];
    [self updateLayoutForOrientation:toInterfaceOrientation];

}
//竖屏
- (void)updateLayoutForOrientation:(UIInterfaceOrientation)orientation{
    CHTCollectionViewWaterfallLayout *layout =
    (CHTCollectionViewWaterfallLayout *)self.ShortNoteCollectionView.collectionViewLayout;
    layout.columnCount = UIInterfaceOrientationIsPortrait(orientation) ? 2 : 4;
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{
    return self.dataArr.count;
}

- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath{
    RMBaseModel *model = [self.dataArr objectAtIndex:indexPath.row];
    RMWriteShortNoteViewController *RWSN = [[RMWriteShortNoteViewController alloc]init];
    RWSN.model = model;
    [self.navigationController pushViewController:RWSN animated:YES];
    
    
}


- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    self.cell = [collectionView dequeueReusableCellWithReuseIdentifier:reuseIdentifier
                                                                           forIndexPath:indexPath];
    RMBaseModel *model = [self.dataArr objectAtIndex:indexPath.row];
    self.cell.contentLabel.lineBreakMode = UILineBreakModeWordWrap;
    self.cell.contentLabel.numberOfLines = 0;   
    self.cell.contentLabel.text = model.content;

    self.cell.TimeLabel.text = [NSString stringWithFormat:@"%@.%@.%@",model.createDate.year,model.createDate.month,model.createDate.day];
    self.cell.deleteButton.hidden = true;
    self.cell.model = model;
    if (model.isLove) {
        [self.cell.LoveButton setImage:[UIImage imageNamed:@"star1.png"] forState:(UIControlStateNormal)];
    }else{
        [self.cell.LoveButton setImage:[UIImage imageNamed:@"star2.png"] forState:(UIControlStateNormal)];
           }
    

    return self.cell;
}

- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath {
   
    RMBaseModel *model = [self.dataArr objectAtIndex:indexPath.row];    
    UIFont *fnt = [UIFont systemFontOfSize:kRealValue(18)];
    CGRect tmpRect = [model.content boundingRectWithSize:CGSizeMake(147,120) options:NSStringDrawingUsesLineFragmentOrigin attributes:[NSDictionary dictionaryWithObjectsAndKeys:fnt,NSFontAttributeName, nil] context:nil];
    return CGSizeMake(147, tmpRect.size.height + 90);
}

- (void)LongPressAction:(UILongPressGestureRecognizer *)sender{
    CGPoint point = [sender locationInView:self.ShortNoteCollectionView];
    NSIndexPath *indexpath = [self.ShortNoteCollectionView indexPathForItemAtPoint:point];
    RMShortNoteCollectionViewCell *cell = (RMShortNoteCollectionViewCell *)[self.ShortNoteCollectionView cellForItemAtIndexPath:indexpath];
    cell.deleteButton.hidden = false;
    RMBaseModel *model = [self.dataArr objectAtIndex:indexpath.row];
    cell.createDate = model.createDate;
    cell.reloaData = ^(){
        _dataArr =  [[RMDateBaseManager defaultManager]selectNoteFromTable:kRMShortNoteTable].mutableCopy;
        [_ShortNoteCollectionView reloadData];
    };
}


/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/


@end

